<?php

namespace wechat\src;

/**
 * 微信支付，统一操作方法类
 */
use wechat\Wechat;

class Pay extends SrcAbstract {

    /**
     * 设置参数
     */
    public function __construct($options) {
        list($this->mchid, $this->mchkey) = $options;
    }

    /**
     * 统一下单
     */
    public function unifiedorder($options, $tradeType = 'JSAPI') {
        $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        $nonceStr = wxRandom(32);
        $params = [
            'appid' => $this->wechat->appid,
            'mch_id' => $this->mchid,
            'nonce_str' => $nonceStr,
            'sign_type' => 'MD5',
            'body' => "扫二维码付款",
            'detail' => "扫二维码付款",
            'out_trade_no' => '',
            'fee_type' => 'CNY',
            'total_fee' => 1,
            'notify_url' => url('api2/payment/notify', [], true, true),
            'trade_type' => $tradeType,
            'openid' => '',
        ];
        $options = array_merge($params, $options);
        $options['sign'] = $this->MakeSign($options, $this->mchkey);
        $result = HttpRequest($url, ArrayToXml($options), true);
        $resultArray = ArrayFromXml($result);
        var_dump($resultArray);
        if (strtolower($resultArray['return_code']) == 'success' && strtolower($resultArray['result_code']) == 'success') {
            switch ($tradeType) {
                case 'JSAPI':
                    $timestamp = time();
                    $signData = $returnData = [
                        'timeStamp' => (string) $timestamp,
                        'nonceStr' => random(32),
                        'package' => "prepay_id={$resultArray['prepay_id']}",
                        'signType' => 'MD5',
                    ];
                    $signData['appId'] = $this->wechat->appid;
                    $returnData['paySign'] = $this->MakeSign($signData, $this->mchkey);
                    break;
                default:
                    $returnData = $resultArray;
            }
            return $returnData;
        } else {
            $this->setError(102, "微信下单失败:{$resultArray['return_msg']}");
            return false;
        }
    }

    /**
     * 回调验证方法
     * @return 订单信息
     */
    public function notify() {
        $xml = file_get_contents("php://input");
        $result = ArrayFromXml($xml);
        if (empty($result)) {
            $this->setError(103, "微信通知数据未取得");
            return false;
        }
        //签名验证
        if ($result['sign'] == $this->MakeSign($result, $this->mchkey)) {
            $this->setError(0, "验签成功");
            return $result;
        } else {
            $this->setError(103, "签名验证失败");
            return false;
        }
    }

    /**
     * 向微信返回异步处理结果
     */
    public function notifyResult() {
        $errCode = $this->getErrCode();
        $errMsg = $this->getErrMsg();
        if (empty($errCode)) {
            $result['return_code'] = 'SUCCESS';
            $result['return_msg'] = "OK";
        } else {
            $result['return_code'] = 'FAIL';
            $result['return_msg'] = $errMsg;
        }
        return ArrayToXml($result);
    }

}
